package com.sayimo.maker.activity.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.activity.dao.BaseGroupBuyDao;
import com.sayimo.maker.activity.service.BaseGroupBuyService;
import com.sayimo.maker.activity.vo.BaseGroupBuy;
import com.sayimo.maker.commons.Constant;
//import com.sayimo.school.mall.module.task.GroupBuyJob;

/**
 * @Copyright (C)  广东省尚一互联网有限公司
 * @Project: schoolmallapi
 * @ClassName: BaseGroupBuyServiceImpl
 * @Description: 
 * @author:	wangyang
 * @date:	日期：2016年10月18日  时间：上午10:47:59
 * @version 2.0.0
 */
@Service(value="baseGroupBuyService")
public class BaseGroupBuyServiceImpl implements BaseGroupBuyService, ApplicationContextAware{
	private static Log logger = LogFactory.getLog(BaseGroupBuyServiceImpl.class);
	@Autowired
	private BaseGroupBuyDao baseGroupBuyDao;
	private ApplicationContext applicationContext;
	
	private static final String GROUP_NAME = "GROUP";
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	/** 
	  * @package com.sayimo.school.mall.module.task
	  * @author wangyang
	  * @Title:  
	  * @Description: 处理已过期的团购活动
	  * @param 
	  * @throws 
	  * @return void
	  * @date 2016年10月19日
	  */ 
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Object selectGroupBuyGoodsList(String _goodsType)
			throws ServiceException {
		Map resultMap = new HashMap();
		if(StringUtils.isEmpty(_goodsType)){//参数错误
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR, Constant.SYSTEM_REQUEST_PARAM_ERROR);
			return resultMap;
		}
		//商品类型 1普通商品 0预约商品
		Integer goodsType=Integer.parseInt(_goodsType);
		try {
			List<BaseGroupBuy> list=new ArrayList<BaseGroupBuy>();
			if(goodsType==1){
				list =baseGroupBuyDao.selectGroupBuyGoodsList(goodsType);
			}else if(goodsType==0){
				list =baseGroupBuyDao.selectGroupBuyPreGoodsList(goodsType);
			}

			resultMap.put(Constant.DATA, list);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return resultMap;
	}
//	/** 
//	  * @package com.sayimo.school.mall.module.task
//	  * @author wangyang
//	  * @Title:  
//	  * @Description: 动态添加定时任务
//	  * @param 
//	  * @throws 
//	  * @return Map
//	  * @date 2016年10月19日
//	  */ 
//	public Map addGroupBuyActivity(String identifier) throws ServiceException {
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//
//		// 动态设置trigger执行时间
//		Scheduler scheduler = (Scheduler) applicationContext.getBean("quartzSchedule");
//		try {
//			Map<String, Object> baseGroupBuy =baseGroupBuyDao.selectBaseGroupBuyIdentifier(identifier);
//			ScheduleJob job = new ScheduleJob();
//			job.setJobId(identifier + "_JOBDETAIL");
//			job.setJobName(identifier + "_JOBDETAIL");
//			job.setJobGroup(GROUP_NAME);
//			job.setJobStatus("1");
//			String endDateString=baseGroupBuy.get("endDate").toString();
//			String month=endDateString.substring(5, 7);
//			String day=endDateString.substring(8, 10);
//			String hour=endDateString.substring(11, 13);
//			String minutes=endDateString.substring(14, 16);
//			String second=endDateString.substring(17, 19);
//			String cronExpressionString=second+" "+minutes+" "+hour+" "+day+" "+month+" ? *";
//			job.setCronExpression(""+cronExpressionString+"");
////			job.setCronExpression("0/10 * * * * ?");
//
//			TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
//			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//			
//			if (trigger == null) {
//				JobDetail jobDetail = JobBuilder.newJob(GroupBuyJob.class)
//						.withIdentity(job.getJobName(), job.getJobGroup())
//						.build();
//				jobDetail.getJobDataMap().put("scheduleJob", job);
//				CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//				trigger = TriggerBuilder.newTrigger()
//						.withIdentity(job.getJobName(), job.getJobGroup())
//						.withSchedule(scheduleBuilder).build();
//				scheduler.scheduleJob(jobDetail, trigger);
//			}
//			logger.info("trigger = " + trigger.getKey());
//		} catch (SchedulerException | DaoException e) {
//			logger.error(e);
//			throw new ServiceException(e);
//		}
//		resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
//		return resultMap;
//	}
//	/** 
//	  * @package com.sayimo.school.mall.module.task
//	  * @author wangyang
//	  * @Title:  
//	  * @Description: 动态修改定时任务
//	  * @param 
//	  * @throws 
//	  * @return Map
//	  * @date 2016年10月19日
//	  */
//	public Map updateGroupBuyActivity(String identifier) throws ServiceException {
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
//		TriggerKey triggerKey = TriggerKey.triggerKey(identifier + "_JOBDETAIL", "GROUP");
//		try { 
//			Scheduler scheduler = (Scheduler) applicationContext.getBean("quartzSchedule");
//			CronTrigger trigger = (CronTriggerImpl) scheduler.getTrigger(triggerKey);
//			if (trigger == null) {
//				return addGroupBuyActivity(identifier);
//			}
//			Map<String, Object> baseGroupBuy =baseGroupBuyDao.selectBaseGroupBuyIdentifier(identifier);
//			String endDateString=baseGroupBuy.get("endDate").toString();
//			String month=endDateString.substring(5, 7);
//			String day=endDateString.substring(8, 10);
//			String hour=endDateString.substring(11, 13);
//			String minutes=endDateString.substring(14, 16);
//			String second=endDateString.substring(17, 19);
//			String cronExpression=second+" "+minutes+" "+hour+" "+day+" "+month+" ? *";
//			if (trigger.getCronExpression().equals(cronExpression)) {
//				return resultMap;
//			}
//			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(""+cronExpression+"");
//			trigger = trigger.getTriggerBuilder().withIdentity(triggerKey)
//				    .withSchedule(scheduleBuilder).build();	
//			scheduler.rescheduleJob(triggerKey, trigger);
//		} catch (SchedulerException | DaoException e) {
//			logger.error(e);
//			throw new ServiceException(e);
//		}
//		return resultMap;
//	}
//	/** 
//	  * @package com.sayimo.school.mall.module.task
//	  * @author wangyang
//	  * @Title:  
//	  * @Description: 动态删除定时任务
//	  * @param 
//	  * @throws 
//	  * @return Map
//	  * @date 2016年10月19日
//	  */
//	public Map deleteGroupBuyActivity(String identifier) throws ServiceException {
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
//		
//		Scheduler scheduler = (Scheduler) applicationContext.getBean("quartzSchedule");
//	
//		JobKey jobKey = JobKey.jobKey(identifier + "_JOBDETAIL", GROUP_NAME);
//		logger.info("jobKey = " + jobKey);
//		if (jobKey == null) {
//			return resultMap;
//		}
//		try {
//			logger.info(scheduler.deleteJob(jobKey));
//		} catch (SchedulerException e) {
//			logger.error(e);
//			throw new ServiceException(e);
//		}
//		return resultMap;
//	}

	/** 
	* @Title: queryIdentifiersByNormal 
	* @Description: 
	* @return
	* @return String
	* @throws 
	*/ 
	
	public String queryIdentifiersByNormal() throws ServiceException {
		try {
		String identifiers = baseGroupBuyDao.queryIdentifiersByNormal();
			return identifiers;
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
	}

	/** 
	* @Title: selectGroupBuyGoodsDeatil 
	* @Description: 团购商品信息
	* @param request
	* @return
	* @return Object
	* @throws 
	*/
	@Override
	public Object selectGroupBuyGoodsDeatil(String identifier)
			throws ServiceException {
		Map resultMap = new HashMap();
		if(StringUtils.isEmpty(identifier)){//参数错误
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR, Constant.SYSTEM_REQUEST_PARAM_ERROR);
			return resultMap;
		}

		try {
			Map<String, Object> dataMap = new HashMap<>();
			if(identifier != null	&& !identifier.equals("") && (identifier.contains("PT_TG_"))){  //普通商品
				dataMap =baseGroupBuyDao.queryInfoByIdentifierGoods(identifier);
			}else if(identifier != null	&& !identifier.equals("") && (identifier.contains("YY_TG_"))){  //预约商品
				dataMap =baseGroupBuyDao.queryInfoByIdentifierPreGoods(identifier);
			}

			resultMap.put(Constant.DATA, dataMap);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
		} catch (DaoException e) {
			logger.info(e);
			e.printStackTrace();
		}
		return resultMap;
	}


}
